package br.com.rodrigo.activemq;

import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.NamingException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import br.com.rodrigo.utils.ConnectionUtils;
import br.com.rodrigo.utils.QueueUtils;

/**
 *
 * <b>Descricao da Classe:</b><br>
 * TODO Explicar detalhadamente proposito da classe
 *
 * @author rodrigo.bacellar
 * @since 17/11/2014
 *
 */
public class Postador
{
    private final Logger logger = LoggerFactory.getLogger(Postador.class);

    public void sendMessage(String msg, String queueName)
    {

        try
        {
            /** CONEXAO */
            QueueConnection conn = ConnectionUtils.getQueueConnection();

            /** SESSAO */
            QueueSession session = conn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

            /** FILA */
            Queue queue = QueueUtils.getQueue(queueName);

            /** PRODUTOR */
            MessageProducer producer = session.createProducer(queue);

            /** MSG */
            TextMessage message = session.createTextMessage();
            message.setText(msg);

            producer.send(message);

            logger.info("Mensagem [{}] postada na fila [{}]", message.getText(), queueName);
        }
        catch (NamingException e)
        {
            e.printStackTrace();
        }
        catch (JMSException e)
        {
            e.printStackTrace();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

    }
}
